/**
 * Created by lil09 on 15-10-16.
 */
define(function (require, exports, module) {
    var $ = require("jquery");
    var ui = require("ui");
    var angular = require("angular");
    var api_prefix = G_CONFIG["api_prefix"]; //api前缀

    var appointmentActivitiesModule = angular.module('appointmentActivitiesModule', []);

    function appointmentActivitiesService($http, $q, $location, $timeout) {
        var service = {
            // 项目列表
            list: [],
            filter: {},
            //城市列表
            filterDataCities: [],
            //编辑页面项目数据
            projectList: [],
            //重新加载数据
            reloadData: function () {
                var p = service.filter;
                service.query(p.city_id || '', p.appointment_type || '');
            },
            //获取城市列表
            getCityList: function () {
                $http.get(api_prefix + '/yykf/appointment-project/get-city-list')
                    .then(function (res) {
                        var retCode = res.data.retCode;
                        if (retCode == 0) {
                            service.filterDataCities = res.data.data;
                            service.filterDataCities.unshift({myscrm_CityId: '', CityName: '全部', selected: true});
                        }
                    });
            },
            //查询列表
            query: function (city, yuyueType) {
                service.filter = {city_id: city, appointment_type: yuyueType};

                $http.get(api_prefix + '/yykf/appointment-project/get-list', {params: service.filter})
                    .then(function (res) {
                        var retCode = res.data.retCode;
                        if (retCode == 0) {
                            service.list = res.data.data;
                        } else {
                            ui.showErrTip(res.data.errMsg);
                            return false;
                        }
                    });
            },
            //打开编辑弹出框
            initEditActivitiesDialog: function ($data, $dialog, $dialogScope, activity) {
                angular.extend($data, {project_id: activity.project_id, appointment_type: activity.appointment_type, banner_url: ""});

                $http.get(api_prefix + '/yykf/appointment-project/get-appointment-project', {params: {project_id: activity.project_id}})
                    .then(function (res) {
                        var retCode = res.data.retCode;
                        if (retCode == 0) {
                            var data = res.data.data;
                            angular.extend($data, {project_id: data.project_id, appointment_type: data.appointment_type, banner_url: data.banner_url});
                            $dialogScope.filterDataYuYue = [
                                {value: '0', name: '普通预约'},
                                {value: '1', name: '专车预约'}
                            ];
                            $dialogScope.filterDataYuYue[$data.appointment_type].selected = true;
                        } else {
                            ui.showErrTip(res.data.errMsg);
                            return false;
                        }
                    });

                $dialogScope.currYuYue = {value: ''};
                //预约形式选择
                 $dialogScope.search = function () {
                     $data.appointment_type=$dialogScope.currYuYue.value;
                 };

                $dialogScope.uploader = {};

                //上传图片
                $dialogScope.uploadImages = function (status) {
                    switch (status.status) {
                        case 'success':
                            break;
                        case 'error':
                            if (status.msg.toString().match(/File\s+size\s+error/)) {
                                $dialogScope.uploader.state = '图片大小超出限制';
                            } else if (status.msg.toString().match(/File\s+extension\s+error/)) {
                                $dialogScope.uploader.state = '图片类型不符';
                            }
                            else {
                                $dialogScope.uploader.state = status.msg;
                            }
                            break;
                    }
                };
            },
            //保存预约项目
            save: function ($data, $dialog) {
                $http.post(api_prefix + '/yykf/appointment-project/save', $data)
                    .then(function (res) {
                        var retCode = res.data.retCode;
                        if (retCode == 0) {
                            service.reloadData();
                            $dialog.close();
                        } else {
                            ui.showErrTip(res.data.errMsg);
                        }
                    });
                return false;
            }
        };

        return service;
    }

    appointmentActivitiesModule.factory('appointmentActivities', ['$http', '$q', '$location', '$timeout', appointmentActivitiesService]);

    module.exports = appointmentActivitiesModule;

});
